/*
 * This file is part of aion-lightning <aion-lightning.org>.
 *
 *  aion-lightning is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-lightning is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-lightning.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.model.TaskId;
import com.aionengine.gameserver.model.gameobjects.LetterType;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.spawnengine.VisibleObjectSpawner;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.utils.ThreadPoolManager;

import java.util.concurrent.Future;

/**
 * @author antness thx to Guapo for sniffing
 */
public class CM_READ_EXPRESS_MAIL extends AionClientPacket {

    private int action;

    public CM_READ_EXPRESS_MAIL(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    @Override
    protected void readImpl() {
        this.action = readC();
    }

    @Override
    protected void runImpl() {

        final Player player = getConnection().getActivePlayer();
        boolean haveUnreadExpress = (player.getMailbox().haveUnreadByType(LetterType.EXPRESS) || player.getMailbox().haveUnreadByType(LetterType.BLACKCLOUD));
        switch (this.action) {
            case 0:
                // window is closed
                if (player.getPostman() != null) {
                    player.getPostman().getController().onDelete();
                    player.setPostman(null);
                }
                break;
            case 1:
                // click on icon
                if (player.getPostman() != null) {
                    PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_POSTMAN_ALREADY_SUMMONED);
                } else if (player.isFlying()) {
                    PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_POSTMAN_UNABLE_IN_FLIGHT);
                } else if (player.getController().hasTask(TaskId.EXPRESS_MAIL_USE)) {
                    PacketSendUtility.sendPacket(player, SM_SYSTEM_MESSAGE.STR_POSTMAN_UNABLE_IN_COOLTIME);
                } else if (haveUnreadExpress) {
                    VisibleObjectSpawner.spawnPostman(player);
                    Future<?> task = ThreadPoolManager.getInstance().schedule(new Runnable() {
                        @Override
                        public void run() {
                        }

                    }, 600000); // 10 min
                    player.getController().addTask(TaskId.EXPRESS_MAIL_USE, task);
                }
                break;
        }
    }

}
